using EnvDTE;
using Microsoft.Practices.ComponentModel;
using Microsoft.Practices.RecipeFramework;

namespace PrismFactory.SoftwareFactory.Extensions.ValueProviders.VisualStudio
{
    /// <summary>
    /// ValueProvider that returns the first selected project
    /// in the solution explorer
    /// </summary>
    [ServiceDependency(typeof (DTE))]
    public class FirstSelectedProjectValueProvider : ValueProvider
    {
        /// <summary>
        /// Sets the newValue to the first selected project
        /// </summary>
        /// <param name="currentValue"></param>
        /// <param name="newValue"></param>
        /// <returns></returns>
        public override bool OnBeginRecipe(object currentValue, out object newValue)
        {
            newValue = currentValue;
            var vs = (DTE) GetService(typeof (DTE));
            var activeProjects = (object[]) vs.ActiveSolutionProjects;
            if (activeProjects != null && activeProjects.Length > 0)
            {
                newValue = activeProjects[0] as Project;
            }
            if (newValue != null)
            {
                return true;
            }
            return false;
        }
    }
}